/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ch.bbcnet.awesomeplayer;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.util.Arrays;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;



/**
 *
 * @author eburgs
 */
public class Tagaendern extends JFrame implements ActionListener{

    //Variablendeklaration
    XML xml = XML.getInstance();
    Tags t = Tags.getInstance();
    Table.MyTableModel m;
    
    long test;
    public JMenuItem item;
    static Object[][] data = {};
    public final String [] Names;
    public final String[] Paths;
    Container cP;
    
    //Buttons und Panels
     JButton OK = new JButton("OK");
     JButton Abb = new JButton("Abbrechen");
    
     JTextField Tit = new JTextField();
     JTextField Alb = new JTextField();
     JTextField Aut = new JTextField();
     JTextField Gen = new JTextField();
     
     JPanel panel1 = new JPanel(new BorderLayout());
     JPanel panel2 = new JPanel(new GridLayout(4,2));
     JLabel l1 = new JLabel();
     JLabel l2 = new JLabel();
     JLabel l3 = new JLabel();
     JLabel l4 = new JLabel();
     
     Table listenGUI;
           
     /**
      * 
      * @param l
      * @param Name
      * @param Artists
      * @param Albums
      * @param Genres
      * @param Path <br />
      * Requires String[]<br />
      * Changes Tags in XML
      * 
      */
     
    public Tagaendern(Table l,String[] Name, String [] Artists, String [] Albums, String[] Genres, String[] Path){
        
        //Fenster Bearbeiten
     Image img = new ImageIcon(getClass().getResource("/ch/bbcnet/awesomeplayer/files/logo2.png")).getImage();
     setIconImage(img);        
     listenGUI = l;
     Names = Name;
     Paths = Path;
     setSize(300,200);
     setLocation(300,150);
     setTitle("Tag Bearbeiten");
     setResizable(false);
     
     //Liste für Tabelle erstellen
    data = new Object[Names.length][4];
    for(int x = 0;x<Names.length;x++){
     String [] tmp = {
         Names[x],Artists[x], Albums[x],Genres[x], Paths[x]};
     data[x] = tmp;
    }
    
    //AL, Panels und Textfelder
     OK.addActionListener(this);
     Abb.addActionListener(this);
        
     panel1.add(OK,BorderLayout.WEST);
     panel1.add(Abb,BorderLayout.EAST);
     
     l1.setText("Titel:");
     l1.setHorizontalAlignment(JLabel.CENTER);
     l1.setVerticalAlignment(JLabel.CENTER);
     panel2.add(l1);
     
     if(Names.length>1){
         Tit.setEditable(false);
     }else{
         Tit.setText(data[0][0].toString());
     }panel2.add(Tit);
     
     l2.setText("Album:");
     l2.setHorizontalAlignment(JLabel.CENTER);
     l2.setVerticalAlignment(JLabel.CENTER);
     panel2.add(l2);
     
     Alb.setText(data[0][2].toString());
     panel2.add(Alb);
   
     l3.setText("Interpret:");
     l3.setHorizontalAlignment(JLabel.CENTER);
     l3.setVerticalAlignment(JLabel.CENTER);
     panel2.add(l3);
     
     Aut.setText(data[0][1].toString());
     panel2.add(Aut);
     
     l4.setText("Genre:");
     l4.setHorizontalAlignment(JLabel.CENTER);
     l4.setVerticalAlignment(JLabel.CENTER);
     panel2.add(l4);
     
     Gen.setText(data[0][3].toString());
     panel2.add(Gen);
     
     cP = this.getContentPane();
     cP.setLayout(new BorderLayout());
     cP.add(panel1,BorderLayout.SOUTH);
     cP.add(panel2,BorderLayout.CENTER);
                 

    
    setVisible(true);
    
    
    

       
    
     }

    
    @Override
    public void actionPerformed(ActionEvent e) {
        
        //OK-Button
        if(e.getSource()==OK){
            for(int i = 0;i<Names.length;i++){
                if(Names.length==1){
                    //System.out.println("ÜbergabePfad= "+data[i][4].toString()+" Titel: "+Tit.getText()+" Autor: "+Aut.getText()+" Album: "+Alb.getText()+" Genre: "+Gen.getText());
                    //System.out.println(Arrays.deepToString(new Object[]{Tit.getText(),Aut.getText(),Alb.getText(),Gen.getText()}));
                    xml.updateInfos(data[i][4].toString(),new Object[]{Tit.getText(),Aut.getText(),Alb.getText(),Gen.getText()});
                }else{
                    xml.updateInfos(data[i][4].toString(), new Object[]{Names[i],Aut.getText(),Alb.getText(),Gen.getText()});
                }
                

            }
            listenGUI.setList(xml.getData(listenGUI.nowShowing), listenGUI.nowShowing);    
            this.dispose();
                }
            //Abbrechen-Button
        else{
            this.dispose();
        }

            }
        }
